package com.gxa.meiye.admin.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.meiye.admin.platform.mapper.WebUserMapper;
import com.gxa.meiye.admin.platform.pojo.domain.User;
import com.gxa.meiye.admin.platform.pojo.vo.AddWebUserVo;
import com.gxa.meiye.admin.platform.pojo.vo.UpdateWebUserStatusVo;
import com.gxa.meiye.admin.platform.pojo.vo.WebUserVo;
import com.gxa.meiye.admin.platform.service.WebUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WebUserServiceImpl extends ServiceImpl<WebUserMapper, WebUserVo> implements WebUserService {

    @Autowired
    private WebUserMapper webUserMapper;

    @Override
    public List<Map<String, Object>> webUserList() {
        List<Map<String, Object>> maps = webUserMapper.webUserList();
        return maps;
    }

    @Override
    public Integer addWebUser(AddWebUserVo userVo) {
        User user = new User();
        user.setAccount(userVo.getAccount());
        user.setName(userVo.getName());
        user.setPhone(userVo.getPhone());
        user.setPwd(userVo.getPwd());
        user.setStatus(userVo.getStatus());
        webUserMapper.add(user);
        Integer r_id = webUserMapper.selectRoleId(userVo.getRoleName());
        return webUserMapper.addUserRole(user.getId(), r_id);
    }

    @Override
    public List<String> roleList() {
        List<String> strings = webUserMapper.roleList();
        return strings;
    }

    @Override
    public List<WebUserVo> selectUsers(String select) {
        List<User> maps = webUserMapper.queryList("%"+select+"%");
        List<WebUserVo> webUserVos = new ArrayList<>();
        for (User user:maps) {
            List<String> strings = webUserMapper.selectNameByUserId(user.getId());
            for (int i = 0; i <strings.size() ; i++) {
                WebUserVo webUserVo = new WebUserVo();
                webUserVo.setId(user.getId());
                webUserVo.setAccount(user.getAccount());
                webUserVo.setName(user.getName());
                webUserVo.setPhone(user.getPhone());
                webUserVo.setRoleName(strings.get(i));
                webUserVo.setStatus(user.getStatus());
                webUserVos.add(webUserVo);
            }
        }
        return webUserVos;
    }

    @Override
    public void updateStatus(UpdateWebUserStatusVo userVo) {
        LambdaUpdateWrapper<WebUserVo> userVoLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        userVoLambdaUpdateWrapper.eq(WebUserVo::getId,userVo.getId());
        if(userVo.getStatus()!=0) {
            userVoLambdaUpdateWrapper.set(WebUserVo::getStatus, 0);
        }else {
            userVoLambdaUpdateWrapper.set(WebUserVo::getStatus, 1);
        }
        webUserMapper.update(null,userVoLambdaUpdateWrapper);
    }

    @Override
    public void delUser(Integer id) {
        webUserMapper.deleteById(id);
        webUserMapper.delUserRole(id);
    }

    @Override
    public Integer updateUser(AddWebUserVo webUserVo) {
        webUserMapper.update(webUserVo);
        Integer integer = webUserMapper.selectRoleId(webUserVo.getRoleName());
        webUserMapper.delUserRoleId(webUserVo.getId(),webUserVo.getName());
        Integer update = webUserMapper.addUserRole(webUserVo.getId(), integer);
        return update;
    }
}
